<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            let tree = {
                val: 1,
                left: {
                    val: 2,
                    left: {
                        val: 4,
                    },
                    right: {
                        val: 5,
                    },
                },
                right: {
                    val: 3,
                    left: {
                        val: 6,
                    },
                },
            }
            function allPath(root) {
                if (!root) return []
                let res = []

                function backTacking(root, path) {
                    if (!root.left && !root.right) {
                        return res.push([...path])
                    }
                    if (root.left) {
                        path.push(root.left.val)
                        backTacking(root.left, path)
                        path.pop()
                    }
                    if (root.right) {
                        path.push(root.right.val)
                        backTacking(root.right, path)
                        path.pop()
                    }
                }
                backTacking(root, [root.val])
                console.log(res)
            }
            allPath(tree)
        </script>
    </body>
</html>
